(function () {
  //商品模板
  var sGoodsTemplate = '';
  var oGoodsUl = document.getElementById("goods_ul");
  var goodsNav = document.getElementById("goods_nav");
    var oGoodsCategory = document.getElementById("goods_category");

  /**
   * 处理商品
   */
  var doGoods = function(d) {
      ucshop.clearRange();
      if (d==0){
          alert("没有更多的数据了！");
          return;
      }

    try {
        oGoodsUl.innerHTML=null;
      if (d==0){
        document.querySelector(".goods_container").innerHTML="数据为空";
        return;
      }
      $.each(d, function (item) {
          var discount= item.discount=='0'?1:(parseFloat(item.discount).toFixed(1)/10);
          item.discountPrice=parseFloat(item.price*discount).toFixed(2);
        var sLi = $.compile(sHotGoodsTemplate, item);
        oGoodsUl.innerHTML += sLi;
      });
    } catch(e) {
      console.log(e.errors);
    }
  };
  /**
   * 获取商品
   */

    var classID = $.getQueryString("classID");
    var searchText=$.getQueryString("searchText");
  var getGoods = function(pages){

      var tem=classID?{"classID": classID, "pageCode":pages, "linenumber":12,"format": "jsonp", "callback": "doGoods"}:{"selectText": searchText, "pageCode":pages, "linenumber":12,"format": "jsonp", "callback": "doGoods"};
      var url=searchText?ucshop.searchGoodsAddress():ucshop.getGoodsAddress();
    $.loadTemplate('./template/list_goods.html', function(err, data){
      if (err) {
        console.log(err);
        return;
      }
      sHotGoodsTemplate = data;
        $.jsonp(url, tem, 'doGoods', doGoods);
    });
  };

  /**
   * 处理商品分类数据
   */

  var doGoodsCategory = function(){
      var classID = $.getQueryString("classID");
      var goods=JSON.parse(window.sessionStorage.goods);
      var catName=goods[classID];
      var navResult=catName || "收索结果为："+searchText;
      $.children(goodsNav,1).innerHTML=navResult;
      var oGoodsCategoryTempt="";
      $.map(goods, function(key){
          oGoodsCategoryTempt +="<li><a href='list.html?classID="+key+"'><img src='./images/"+key+".png' width='50px'><p>"+goods[key]+"</p></a></li>"
      });
      oGoodsCategory.innerHTML=oGoodsCategoryTempt;
  };
  /**
   * 获取商品分类信息
   */
  window.onload = function() {
    ucshop.checkLoginStatus();
    ucshop.toTop();
    ucshop.searchGoods();
    ucshop.slide_up();
    ucshop.progress();
    ucshop.pageShow(getGoods);
    //获取商品分类信息
    doGoodsCategory();
    //获取商品
    getGoods(0);

  }
})();